package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;
import java.util.stream.Collectors;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    //开始搜索
    @GetMapping("/list.html")//?keyword=            SearchParam对象能直接获取到?能
    public String list(Model model, SearchParam searchParam) {
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //1.回显关键词
        model.addAttribute("searchParam", searchParam);
        //2.品牌集合
        model.addAttribute("trademarkList", responseVo.getTrademarkList());
        //3.urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //4.平台属性
        model.addAttribute("attrsList", responseVo.getAttrsList());
        //5.商品集合
        model.addAttribute("goodsList", responseVo.getGoodsList());
        //6.总页数 当前页
        model.addAttribute("pageNo", responseVo.getPageNo());
        model.addAttribute("totalPages", responseVo.getTotalPages());
        //7.排序 综合1 价格2 新品3
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);
        //8.条件回显 如:全部结果>华为>运行内存>屏幕尺寸>手机>
        //8.1显示品牌 //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            model.addAttribute("trademarkParam", "品牌:" + trademark.split(":")[1]);
        }
        //8.2显示平台属性
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);
        return "list/index";
    }

    //显示平台属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //平台属性数组
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            return Arrays.stream(props).map(prop -> {
                Map map = new HashMap<>();
                //格式如:props=23:4G:运行内存
                String[] p = prop.split(":");
                map.put("attrId", p[0]);
                map.put("attrValue", p[1]);
                map.put("attrName", p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap<>();
        //order=1:asc
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] o = order.split(":");
            orderMap.put("type", o[0]);
            orderMap.put("sort", o[1]);
        } else {
            //默认 hoScore desc
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");
        }
        return orderMap;
    }

    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();
        //1:Keyword
        String keyword = searchParam.getKeyword();
        sb.append("http://list.gmall.com/list.html?keyword=" + keyword);
        //2:品牌 品牌的Id:品牌的名称
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=" + trademark);
        }
        //3:平台属性
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=" + prop);
            }
        }
        //4:一二三级分类 暂时不写了
        return sb.toString();
    }
}
